<?php

require("class.phpmailer.php");

class BasicMail extends PHPMailer {
	
	var $sUploadPath;
	var $sTempPath;
	var $sFileName;

	/**
	* Contructor
	*
	*/
	function __construct() {						
		
		
	}
	
	function setSubject($sSubject) {
		$this->Subject = $sSubject;
	}
	
	function setCharSet($sCharSet) {
		$this->CharSet = $sCharSet;
	}
	
	function setUseHTML($bUseHTML = true) {
		$this->IsHTML($bUseHTML);
	}
	
	/**
	* Ham dung de thiet lap cac thong so gui theo SMTP
	*
	* @param string $sSendMode	
	* @param string $sHost
	* @param string $sPort	
	* @param string $sUserName	 
	* @param string $sPassword	 	
	*
	*/
	function setSendMode($sSendMode,$sHost = 'localhost',$sPort = '25',$sUserName = '',$sPassword = '') {	
		
		switch($sSendMode) {
			case "smtp":
				$this->IsSMTP();
				break;
			case "mail":
				$this->IsMail();
				break;
			case "sendmail":
				$this->IsSendmail();
				break;
			default:
				$this->IsQmail();
				break;			
		}
				
		$this->Host     = $sHost; // SMTP servers
		$this->Port     = $sPort; // SMTP servers		
		$this->Username = $sUserName;  // SMTP username
		$this->Password = $sPassword; // SMTP password		
		if ($this->Username)
			$this->SMTPAuth = true;     // turn on SMTP authentication			
		else
			$this->SMTPAuth = false;     // turn on SMTP authentication				
	}				
	
	/**
	* Ham dung de add cac mail cho addcc
	*
	* @param string $sCCList
	* @param string $sSplitMail
	* @param string $sSplitName		
	*
	*/
	function setAddCC($sCCList,$sSplitMail = ";",$sSplitName = ">") {
	
		if ($sCCList) {						
			$aCC = explode($sSplitMail,$sCCList.";");
			if (is_array($aCC)) {
				foreach ($aCC as $key => $value) {						
					if (substr_count($value,$sSplitName) > 0 ) {
						$aCCName = explode($sSplitName,$value);	
						if (is_array($aCCName)) {
							$this->AddCC($aCCName[1],substr($aCCName[0],1));																					
						}
					}			
					else {			
						if ($value)	{
						
							$this->AddCC($value,$value);	
						}							
					}	
				}
			}		
						
		}		
	}
	
	/**
	* Ham dung de add cac mail cho addbcc
	*
	* @param string $sBCCList
	* @param string $sSplitMail
	* @param string $sSplitName		
	*
	*/
	function setAddBCC($sBCCList,$sSplitMail = ";",$sSplitName = ">") {
		if ($sBCCList) {			
			$aBCC = explode($sSplitMail,$sBCCList);
			if (is_array($aBCC)) {
				foreach ($aBCC as $key => $value) {						
					if (substr_count($value,$sSplitName) > 0 ) {
						$aBCCName = explode($sSplitName,$value);	
						if (is_array($aBCCName)) {
							$this->AddCC($aBCCName[1],substr($aBCCName[0],1));															
						}
					}								
					else {
						if ($value)	
							$this->AddBCC($value,$value);
					}	
				}
			}				
		}			
	}
	
	/**
	* Ham dung de upload attach file den thu muc co dinh va sau do goi ham AddAttachment cua phpmailer
	*
	*/
	function AddAttachFile() {								
						
		if (!file_exists($this->sUploadPath)) {
		   mkdir($this->sUploadPath);
		   chmod($this->sUploadPath, 0777);
		}				
		
		if (!move_uploaded_file($this->sTempPath,$this->sUploadPath."/".$this->sFileName)) {
			$this->showError("Tập tin không upload được !!!");
			$this->showError();
		}	
		
		$this->AddAttachment($this->sUploadPath."/".$this->sFileName,$this->sFileName,"base64","application/octet-stream");
	}
	
	function deleteAttachFile() {
		if (file_exists($this->sUploadPath."/".$this->sFileName)) {
			unlink($this->sUploadPath."/".$this->sFileName);
		}
	}

	/**
	* Ham dung de hien thi loi
	*	
	*/
	 function showError() {
		print $this->ErrorInfo;
		exit();
	}
}

?>